package com.gz.mylife.app.keys;

import com.google.gson.Gson;
import com.gz.mylife.R;
import com.gz.mylife.app.BaseActivity;
import com.gz.mylife.http.AsyncHttpClient;
import com.gz.mylife.http.AsyncHttpResponseHandler;
import com.gz.mylife.http.RequestParams;
import com.gz.mylife.util.PrefUtils;
import com.zl.smartmall.library.po.LoginData;
import com.zl.smartmall.library.util.Constants;

import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginDoorActivity extends BaseActivity implements OnClickListener {

	private EditText et_username;
	private EditText et_pwd;
	private String phone_num;
	private String phone_password;
	private String phone_sn;
	private TextView tv_register;// 注册账号
	private TextView tv_forget_password;//忘记密码
	private Button btn_login;//登录按钮

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_doorlogin);
		setActionbarTitle("门禁登录", false);
		et_username = (EditText) findViewById(R.id.et_acitvenum);
		et_pwd = (EditText) findViewById(R.id.et_phone);
		tv_register = (TextView) findViewById(R.id.tv_register);
		tv_register.setOnClickListener(this);
		tv_forget_password = (TextView) findViewById(R.id.tv_forget_password);
		tv_forget_password.setOnClickListener(this);
		btn_login = (Button) findViewById(R.id.btn_login);
		btn_login.setOnClickListener(this);
		TelephonyManager telephonyMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
		phone_sn = telephonyMgr.getDeviceId();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tv_register:
			Intent i = new Intent(this,RegisterActivity.class);
			startActivity(i);
			break;
		case R.id.tv_forget_password:
			Intent intent = new Intent(this, FindActivity.class);
			startActivity(intent);
			break;
		case R.id.btn_login:
			PrefUtils.removeStrPrefData(LoginDoorActivity.this, Constants.KEYS_URL);
			String strPrefData = PrefUtils.getStrPrefData(LoginDoorActivity.this, Constants.KEYS_URL, "-1");// 读缓存
			phone_num = et_username.getText().toString().trim();
			phone_password = et_pwd.getText().toString().trim();
			if (TextUtils.isEmpty(phone_num) || TextUtils.isEmpty(phone_password)) {
				Toast.makeText(this, "请输入手机号密码！！！", 0).show();
				return;
			}
			String url = Constants.LOGIN_URL;
			AsyncHttpClient httpClient = new AsyncHttpClient();
			RequestParams params = new RequestParams();
			params.put("phone_num", phone_num);
			params.put("phone_password", phone_password);
			params.put("phone_sn", phone_sn);
			Toast.makeText(this, "正在登陆...", 0).show();
			// 这个是get方法请求，自己调配，2者写一就行
			httpClient.get(url, params, new AsyncHttpResponseHandler() {

				@Override
				public void onSuccess(String content) {
					super.onSuccess(content);

					Gson gson = new Gson();
					LoginData mLoginData = gson.fromJson(content, LoginData.class);
					// 验证成功
					if (mLoginData.ref == 0) {
						Toast.makeText(LoginDoorActivity.this, mLoginData.msg, 0).show();
						// 保存手机号&密码
						PrefUtils.setStrPrefData(LoginDoorActivity.this,"phone_num", phone_num);
						PrefUtils.setStrPrefData(LoginDoorActivity.this,"phone_password", phone_password);
						PrefUtils.setStrPrefData(LoginDoorActivity.this,"phone_sn", phone_sn);
						Intent i = new Intent(LoginDoorActivity.this,MainActivity.class);
						startActivity(i);
						finish();
					} else {
						Toast.makeText(LoginDoorActivity.this, mLoginData.msg, 0).show();
					}
				}
				@Override
				public void onFailure(Throwable error, String content) {
					super.onFailure(error, content);
					Toast.makeText(LoginDoorActivity.this, "登录失败，请检查网络", 0).show();
				}
			});
			break;
			
		}
	}

}
